package cn.jetpiece.cloud.system.dp.app.modular.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.jetpiece.cloud.core.page.PageFactory;
import cn.jetpiece.cloud.core.query.QueryGenerator;
import cn.jetpiece.cloud.core.util.HttpContext;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import cn.jetpiece.cloud.starter.app.modular.base.controller.BaseController;
import cn.jetpiece.cloud.system.dp.api.biz.DpTProjectApi;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.CreateDpTProjectParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.UpdateDpTProjectParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.query.DpTProjectQuery;
import cn.jetpiece.cloud.system.dp.api.biz.model.result.DpTAppResult;
import cn.jetpiece.cloud.system.dp.api.biz.model.result.DpTProjectResult;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTApp;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTProject;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTScopeData;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTProjectService;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTScopeDataService;
import cn.jetpiece.cloud.system.dp.app.modular.biz.wrapper.DpTAppWrapper;
import cn.jetpiece.cloud.system.dp.app.modular.biz.wrapper.DpTProjectWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * dp_t_project
 *
 * @author jetpiece
 * @since 2023-05-15
 */
@Slf4j
@RestController
public class DpTProjectController extends BaseController<DpTProject, IDpTProjectService> implements DpTProjectApi {

    @Autowired
    private IDpTProjectService dpTProjectService;

    @Autowired
    private IDpTScopeDataService dpTScopeDataService;

    @Override
    public Result<String> add(CreateDpTProjectParam param) {
        try {
            DpTProject dpTProject = BeanUtil.toBean(param, DpTProject.class);
            if (dpTProjectService.save(dpTProject)) {
                return Result.OK("添加成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("添加失败");
    }

    @Override
    public Result<String> update(UpdateDpTProjectParam param) {
        try {
            DpTProject oldBean = dpTProjectService.getById(param.getProjectId());
            if (oldBean == null) {
                return Result.error("数据未找到!");
            }
            DpTProject dpTProject = BeanUtil.toBean(param, DpTProject.class);
            if (dpTProjectService.updateById(dpTProject)) {
                return Result.OK("编辑成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("编辑失败");
    }

    @Override
    public Result<String> delete(Long id) {
        try {
            if (dpTProjectService.removeById(id)) {
                return Result.OK("删除成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("删除失败");
    }

    @Override
    public Result<String> deleteBatch(String ids) {
        try {
            List<Integer> idList = Convert.toList(Integer.class, ids.split(","));
            if (dpTProjectService.removeByIds(idList)) {
                return Result.OK("批量删除成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("批量删除失败");
    }

    @Override
    public Result<DpTProjectResult> get(Long id) {
        try {
            DpTProject dpTProject = dpTProjectService.getById(id);
            if (dpTProject == null) {
                return Result.error("数据未找到!");
            }
            return Result.OK(BeanUtil.toBean(dpTProject, DpTProjectResult.class));
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("获取数据失败！");
    }

    @Override
    public Result<PageResult<DpTProjectResult>> page(@Valid DpTProjectQuery query) {
        DpTProject dpTProject = BeanUtil.toBean(query, DpTProject.class);
        QueryWrapper<DpTProject> queryWrapper = QueryGenerator.initQueryWrapper(dpTProject, HttpContext.getRequestParameters());
        if (StringUtils.isNotBlank(query.getAppKey())) {
            DpTScopeData scopeData = dpTScopeDataService.getByAppKey(query.getAppKey());
            if (scopeData != null && StringUtils.isNotBlank(scopeData.getProjectValue())) {
                List<String> projectList = new ArrayList<>();
                projectList.addAll(Arrays.asList(scopeData.getProjectValue().split(",")));
                queryWrapper.notIn("project_id", projectList);
            }
        }
        Page<DpTProject> page = PageFactory.defaultPage();
        IPage<DpTProject> pageList = dpTProjectService.page(page, queryWrapper);
        return Result.OK(new DpTProjectWrapper(pageList).setWrapper(true).wrapPageResult());
    }
}
